<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Mail;
use DB;
use Crypt;


class ResetController extends Controller
{
    //forget
    public function forget()
    {
    	return view('admin/reset/forget');
    }

    //sendEmail
    public function sendEmail(Request $request)
    {
    	// dd($request -> all());
    	
        $data = $request -> except('_token');
        
        
  //   	Mail::raw('这是一封来自tianxia.com的邮件', function ($message) 
  //   	{
  //   		$message -> subject('找回密码');
  //   		$message -> to('964152486@qq.com');
		// });

        if($data['auth'] == 0)
        {
            
            //检测 管理员邮箱 是否存在
            $res = DB::table('admins')
                -> where('email', $data['email'])
                -> first();
            
            if(!$res)
            {
                return back() -> with(['info' => '你输入的邮箱不存在']);
            }

            $sus = Mail::send('emails.resetPass', ['res' => $res], function($message) use ($res){
                $message -> subject('找回密码');
                $message -> to($res -> email);
            });

            if($sus)
            {
                return view('admin.reset.info', ['info' =>'邮件发送成功!']);
            }

        }else
        {
            //检测 商家邮箱 是否存在
            $res = DB::table('businesses')
                -> where('email', $data['email'])
                -> first();
            
            if(!$res)
            {
                return back() -> with(['info' => '你输入的邮箱不存在']);
            }

            $sus = Mail::send('emails.businessPass', ['res' => $res], function($message) use ($res){
                $message -> subject('找回密码');
                $message -> to($res -> email);
            });

            if($sus)
            {
                return view('admin.reset.info', ['info' =>'邮件发送成功!']);
            }
        }
        
    }
    
    // //auth

    // public function inputpass()
    // {
    //     return view('admin/reset/inputpass');
    // }



    //newPass
    public function newPass($id, $token)
    {
    	// dd($request -> all());
        //查询 管理员表
            $res = DB::table('admins')
                -> where('id', $id)
                -> where('remember_token', $token)
                -> first();
            if(!$res)
            {
                return view('admin.reset.info', ['info' =>'这不是一个合法来源,请静静地走开']);
            }else
            {
                return view('admin.reset.inputpass', ['res' => $res]);
            }

    }

    public function newBusPass($id, $token)
    {

        //查询 商家表
            $res = DB::table('businesses')
                -> where('id', $id)
                -> where('remember_token', $token)
                -> first();
            if(!$res)
            {
                return view('admin.reset.info', ['info' =>'这不是一个合法来源,请静静地走开']);
            }else
            {
                return view('admin.reset.businterpass', ['res' => $res]);
            }

    }


    //renew
    public function renew(Request $request)
    {
    	$data = $request -> except('_token', 'repassword');
    	$this -> validate($request,[
    		'repassword' => 'required|same:password'
    		],[
    		'repassword.required' => '确认密码不能为空',
    		'repassword.same' => '两次密码不一样'
    		]);
        
        //处理数据
        $data['password'] = Crypt::encrypt($data['password']);

    	$res = DB::table('admins') -> where('id', $data['id']) -> update($data);

    	if($res)
    	{
    		return redirect('admin/login');
    	}else
    	{
    		return back() -> with(['info' => '修改密码失败']);
    	}
    }

    //busRenew
    public function busRenew(Request $request)
    {
        $data = $request -> except('_token', 'repassword');
        $this -> validate($request,[
            'repassword' => 'required|same:password'
            ],[
            'repassword.required' => '确认密码不能为空',
            'repassword.same' => '两次密码不一样'
            ]);
        
        //处理数据
        $data['password'] = Crypt::encrypt($data['password']);

        $res = DB::table('businesses') -> where('id', $data['id']) -> update($data);

        if($res)
        {
            return redirect('admin/login');
        }else
        {
            return back() -> with(['info' => '修改密码失败']);
        }
    }

}
